package com.siyougame.common.constant;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class R<T> implements Serializable {
    private Integer status;

    private String msg;
    private T data;


    private R(Integer status) {
        this.status = status;
    }

    private R(Integer status, T data) {
        this.status = status;
        this.data = data;

    }


    private R(Integer status, String msg, T data) {
        this.status = status;


        this.msg = msg;
        this.data = data;
    }

    private R(Integer status, String msg) {
        this.status = status;

        this.msg = msg;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    public Integer getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }


    public T getData() {
        return data;
    }

    public static <T> R<T> createBySuccess() {
        return new R<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> R<T> createBySuccessMessage(String msg) {
        return new R<T>(ResponseCode.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> createBySuccess(T data) {
        return new R<T>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> R<T> createBySuccess(String msg, T data) {
        return new R<T>(ResponseCode.SUCCESS.getCode(), msg, data);
    }


    public static <T> R<T> createByError() {
        return new R<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    public static <T> R<T> createByErrorMessage(String errorMessage) {
        return new R<T>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <T> R<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new R<T>(errorCode, errorMessage);
    }

}
